package com.digital.smartgrow.service.upscale.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.digital.smartgrow.common.util.WebTool;
import com.digital.smartgrow.dto.upscale.UpPlanPageDTO;
import com.digital.smartgrow.entity.datacenter.Teacher;
import com.digital.smartgrow.entity.upscale.UpPlan;
import com.digital.smartgrow.dao.upscale.UpPlanMapper;
import com.digital.smartgrow.service.sysinfo.DictionaryService;
import com.digital.smartgrow.service.upscale.UpPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

/**
 * <p>
 * 晋升规划 服务实现类
 * </p>
 *
 * @author sunyuechao
 * @since 2022-07-12
 */
@Service
public class UpPlanServiceImpl extends ServiceImpl<UpPlanMapper, UpPlan> implements UpPlanService {
    @Autowired
    private DictionaryService dictionaryService;
    @Override
    public Object getListByPage(UpPlanPageDTO pageDTO) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Page<UpPlan> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        QueryWrapper<UpPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getCode()), "code", pageDTO.getCode());
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getName()), "name", pageDTO.getName());
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getSeries()), "series", pageDTO.getSeries());
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getUpLevel()), "upLevel", pageDTO.getUpLevel());
        //queryWrapper.le(StringUtils.isNotBlank(sdf.format(pageDTO.getUpPlan())), "upActual", pageDTO.getUpPlan());//开始日期，借用计划时间
        queryWrapper.ge(pageDTO.getStartDate()!=null, "upPlan", pageDTO.getStartDate());//开始日期ge("age", 18)--->age >= 18
        queryWrapper.le(pageDTO.getEndDate()!=null, "upPlan", pageDTO.getEndDate());//结束日期
        //queryWrapper.ge(StringUtils.isNotBlank(sdf.format(pageDTO.getUpActual())), "upActual", pageDTO.getUpActual());//结束日期，借用完成时间
        queryWrapper.like(StringUtils.isNotBlank(pageDTO.getUpStatus()), "upStatus", pageDTO.getUpStatus());
        return this.page(page, queryWrapper);
    }

    @Override
    public Object getInfo(Integer id) {
        return this.getById(id);
    }

    /**
     * 我的晋升-关键字查寻（关联字典【系列67和级别27】）,从字典中进行like 操作
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Object getListByKey(String keyword, Integer pageNum, Integer pageSize) {
        //- [ ] 我的晋升-关键字查寻（关联字典【系列67和级别27】）
        String upLevel =StringUtils.isNotBlank(keyword)?dictionaryService.selectBykey(27, keyword):null;
        String series = StringUtils.isNotBlank(keyword)?dictionaryService.selectBykey(67, keyword):null;
        Page<UpPlan> page = new Page<>(pageNum,pageSize );
        QueryWrapper<UpPlan> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(series!=null, "series", series);
        queryWrapper.eq(upLevel!=null, "upLevel", upLevel);
//        if (keyword!=null) {
//            queryWrapper
//                    .and(
//                            wrapper ->
//                                    wrapper.like("name", keyword).
//                                            //or().like("code", keyword).
//                                            or().like("series", keyword).
//                                            or().like("college", keyword)
//                                            .or().like("major", keyword)
//                                            .or().like("upPost", keyword)
//                                            .or().like("upLevel", keyword)
//                                            //.or().like("major", keyword)
//
//                    );
//        }
        //增加查询自己
        Teacher currentUser = WebTool.getCurrentUser();
        queryWrapper.eq("code", currentUser.getEmployeeId());//employeeId
        return this.page(page, queryWrapper);
    }
}
